﻿using KingsSharp.Common.Filter;
using KingsSharp.Core.Data.Entity;
using KingsSharp.Core.Data.Extensions;
using KingsSharp.Web.UI;
using CRM.Admin.Contracts.Contract.Logs;
using CRM.Admin.Contracts.Dto.Logs;
using CRM.Admin.Contracts.Model.Logs;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace CRM.Admin.Areas.Logs.Controllers
{
    public class LoginLogController : Controller
    {
        /// <summary>
        /// 登陆日志契约
        /// </summary>
        public ILoginLogService LoginLogService { get; set; }

        public ActionResult LoginLogIndex()
        {
            return View();
        }

        #region 数据功能
        /// <summary>
        /// 获取登陆日志列表
        /// </summary>
        /// <param name="dtos"></param>
        /// <returns></returns>
        public ActionResult GetLoginLogGridData()
        {
            GridRequest request = new GridRequest(Request);
            Expression<Func<LoginLog, bool>> predicate = FilterHelper.GetExpression<LoginLog>(request.FilterGroup);
            int total;
            var data = LoginLogService.ItemLoginlog
                .Where(a => a.IsDeleted == false)
                .Where<LoginLog, Guid>(predicate, request.PageCondition, out total)
                .OrderByDescending(b => b.CreatedTime)
                .ToList();
            var datadto = data.MapToList<LoginLog, LoginLogDto>();
            datadto.ForEach(a => a.Area = IP.GetFullName(a.Ip));
            return Json(new GridData<object>(datadto, total), JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}